<?php
Yii::import('application.components.Reference');
class DofController extends GxController
{
    public function actionCreate()
    {
        if (!Yii::app()->request->isAjaxRequest) return;
        if (isset($_POST) && !empty($_POST)) {
            Yii::import('application.components.Gl');
            $is_new = $_POST['mode'] == 0;
            $dof_id = $_POST['id'];
            $msg = 'DO Final berhasil disimpan.';
            $detils = CJSON::decode($_POST['detil']);
            Yii::import('application.components.U');
            app()->db->autoCommit = false;
            $transaction = Yii::app()->db->beginTransaction();
            try {
                $dof = $is_new ? new Dof : $this->loadModel($dof_id,
                    "Dof");
                $docref = $dof->doc_ref;
                if ($is_new) {
                    $ref = new Reference();
                    $docref = $ref->get_next_reference(DOF);
                }
                foreach ($_POST as $k => $v) {
                    if (is_angka($v)) $v = get_number($v);
                    $_POST['Dof'][$k] = $v;
                }
                $_POST['Dof']['doc_ref'] = $docref;
                $dof->attributes = $_POST['Dof'];
                if (!$dof->save())
                    throw new Exception("Gagal menyimpan DO Final. ".CHtml::errorSummary($dof));
                DofDetil::model()->deleteAll('dof_id  = :dof_id',
                    array(':dof_id' => $dof_id));
                foreach ($detils as $detil) {
                    $dof_detil = new DofDetil;
                    $_POST['DofDetil']['barang_id'] = $detil['barang_id'];
                    $_POST['DofDetil']['sat'] = $detil['sat'];
                    $_POST['DofDetil']['jml_dos'] = get_number($detil['jml_dos']);
                    $_POST['DofDetil']['pcs_dos'] = get_number($detil['pcs_dos']);
                    $_POST['DofDetil']['jml_dof'] = get_number($detil['jml_dof']);
                    $_POST['DofDetil']['pcs_dof'] = get_number($detil['pcs_dof']);
                    $_POST['DofDetil']['jml_sisa'] = get_number($detil['jml_sisa']);
                    $_POST['DofDetil']['pcs_sisa'] = get_number($detil['pcs_sisa']);
                    $_POST['DofDetil']['jml_riil'] = get_number($detil['jml_riil']);
                    $_POST['DofDetil']['pcs_riil'] = get_number($detil['pcs_riil']);
                    $_POST['DofDetil']['jml_selisih'] = get_number($detil['jml_selisih']);
                    $_POST['DofDetil']['pcs_selisih'] = get_number($detil['pcs_selisih']);
                    $_POST['DofDetil']['dof_id'] = $dof->dof_id;
                    $dof_detil->attributes = $_POST['DofDetil'];
                    if (!$dof_detil->save())
                        throw new Exception("Gagal menyimpan detil DO Final. ".CHtml::errorSummary($dof_detil));
                    if ($dof_detil->pcs_sisa != 0) {
                        U::add_stock_moves(DOF, $dof->dof_id, $dof->tgl,
                            $dof_detil->barang_id, GUDANG_PST, $dof_detil->pcs_sisa, $dof->doc_ref,
                            1);
                        U::add_stock_moves(DOF, $dof->dof_id, $dof->tgl,
                            $dof_detil->barang_id, GUDANG_SMT, -$dof_detil->pcs_sisa, $dof->doc_ref,
                            1);
                    }
                    if ($dof_detil->pcs_selisih != 0){
                        $gl = new Gl();
                        $amount = $dof_detil->pcs_selisih * $dof_detil->barang->price_buy;
                        $gl->add_gl(DOF, $dof->dof_id, $dof->tgl, $dof->doc_ref,
                            SELISIHPERSEDIAAN, '', '', $amount);
                        $gl->add_gl(DOF, $dof->dof_id, $dof->tgl, $dof->doc_ref,
                            PERSEDIANBARANGDAGANG, '', '', -$amount);
                        $gl->validate();
                    }
                }
                if ($is_new) {
                    $ref->save(DOF, $dof->dof_id, $docref);
                }
                $transaction->commit();
                $status = true;
            } catch (Exception $ex) {
                $transaction->rollback();
                $status = false;
                $msg = $ex->getMessage();
            }
            echo CJSON::encode(array(
                'success' => $status,
                'msg' => $msg));
            Yii::app()->end();
        }
    }
    public function actionUpdate($id)
    {
        $model = $this->loadModel($id, 'Dof');
        if (isset($_POST) && !empty($_POST)) {
            foreach ($_POST as $k => $v) {
                if (is_angka($v)) $v = get_number($v);
                $_POST['Dof'][$k] = $v;
            }
            $msg = "Data gagal disimpan";
            $model->attributes = $_POST['Dof'];
            if ($model->save()) {
                $status = true;
                $msg = "Data berhasil di simpan dengan id " . $model->dof_id;
            } else {
                $msg .= " ".CHtml::errorSummary($model);
                $status = false;
            }
            if (Yii::app()->request->isAjaxRequest) {
                echo CJSON::encode(array(
                    'success' => $status,
                    'msg' => $msg
                ));
                Yii::app()->end();
            } else {
                $this->redirect(array('view', 'id' => $model->dof_id));
            }
        }
    }
    public function actionCheckdos()
    {
        if (Yii::app()->request->isPostRequest) {
            $status = true;
            try {
                $criteria = new CDbCriteria();
                $criteria->addCondition("salesman_id = :salesman_id");
                $criteria->addCondition("tgl = :tgl");
                $criteria->params = array(':salesman_id' => $_POST['sales'], ':tgl' => $_POST['tgl']);
                $total = Dos::model()->count($criteria);
                $msg = $total > 0;
            } catch (Exception $ex) {
                $status = false;
                $msg = $ex->getMessage();
            }
            echo CJSON::encode(array(
                'success' => $status,
                'msg' => $msg));
            Yii::app()->end();
        } else
            throw new CHttpException(400,
                Yii::t('app', 'Invalid request. Please do not repeat this request again.'));
    }
    public function actionIndex()
    {
        $tgl = $_POST['tgl'];
        $sales = $_POST['sales'];
        $criteria = new CDbCriteria();
        $criteria->addCondition("salesman_id = :salesman_id");
        $criteria->addCondition("tgl = :tgl");
        $criteria->params = array(':salesman_id' => $sales, ':tgl' => $tgl);
        $model = Dof::model()->findAll($criteria);
        $total = Dof::model()->count($criteria);
        $this->renderJson($model, $total);
    }
}